package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.SearchDeptBo;
import com.zhuss.bean.unit.core.company.DeptBean;
import com.zhuss.bean.vo.core.company.ListDeptVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.SelectVo;
import com.zhuss.core.db.dao.ext.DeptDao;
import com.zhuss.core.db.entity.Dept;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询部门 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/06 22:17
 */
@Component("deptRead")
public class DeptRead {
	
	@Autowired
	private DeptDao deptDao;
	
	
	/**
	 * 根据公司查询部门列表
	 * @param 
	 * @return
	 */
	/**
	 * @param companyId
	 * @return
	 */
	public List<SelectVo> listSelectByCompany(Long companyId) {
		List<SelectVo> result = new ArrayList<SelectVo>();
		List<Dept> deptList = listByCompany(companyId);
		for (Dept dept : deptList) {
			SelectVo sv = new SelectVo(dept.getDeptId().intValue(), dept.getName(), "");
			result.add(sv);
		}
		return result;
	}
	
	
	/**
	 * 根据公司查询部门列表
	 * @param 
	 * @return
	 */
	public List<DeptBean> listById(List<Long> deptIdList) {
		List<DeptBean> result = deptDao.listById(deptIdList);
		return result;
	}
	
	/**
	 * 根据公司查询部门列表
	 * @param 
	 * @return
	 */
	public List<Dept> listByCompany(Long companyId) {
		List<Dept> result = deptDao.listAll(companyId);
		return result;
	}
	
	/**
	 * 根据名字查询部门
	 * @param 
	 * @return
	 */
	public List<Dept> list(Long companyId, String name) {
		List<Dept> result = deptDao.listByName(companyId, name);
		return result;
	}
	
	public List<Dept> listNotId(Long companyId, String name, Long deptId) {
		List<Dept> result = new ArrayList<Dept>();
		List<Dept> list = list(companyId, name);
		for (Dept c : list) {
			if (!c.getDeptId().equals(deptId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 部门
	 * @return
	 */
	public Object listDept(Long companyId) {
		List<Dept> deptList = deptDao.listAll(companyId);
		List<ListVo> result = new ArrayList<>();
		transferList(deptList, result);
		return result;
	}
	
	/**
	 * 部门 
	 * @return
	 */
	public Object listDeptId(Long companyId) {
		List<Dept> deptList = deptDao.listAll(companyId);
		List<Long> result = new ArrayList<>();
		transferListId(deptList, result);
		return result;
	}
	
	private void transferListId(List<Dept> deptList, List<Long> result) {
		for (Dept item : deptList) {
			result.add(item.getDeptId());
		}
	}

	private void transferList(List<Dept> deptList, List<ListVo> result) {
		for (Dept item : deptList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getDeptId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 部门列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchDeptBo bo) {
		PageVo<ListDeptVo> result = new PageVo<ListDeptVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(deptDao.list(bo));
        result.setTotal(deptDao.countList(bo));
		return result;
	}
	
	/**
	 * 部门详情
	 * @param deptId
	 * @return
	 * @throws OceanException
	 */
	public Object detail(Long deptId) throws OceanException {
		Object result = deptDao.detail(deptId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
